package com.jz.community.modulemine.packet.model;

import android.content.Context;

import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.modulemine.packet.bean.RedPacketBean;
import com.jz.community.modulemine.packet.net.RedPacketNetApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import io.reactivex.Observable;

public class RedPacketModelImp implements RedPacketModel {
    private Context mContext;

    public RedPacketModelImp(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void initRedPacketData(int status, int page, int size, OnLoadListener<RedPacketBean> onLoadListener) {
        Observable<RedPacketBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, RedPacketNetApi.class)
                .initRedPacketData(status, page, size));
        new RxLoadingWrapper<RedPacketBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }
}
